<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('wechat.navigation.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('wechat.navigation.picture')" prop="picture">
            <el-upload
              class="avatar-uploader"
              :action="$http.adornUrl('/file/picture')"
              :show-file-list="false"
              :headers="upload_headers"
              :on-success="handlePictureSuccess"
              :before-upload="beforePictureUpload">
              <img v-if="dataForm.picture" :src="dataForm.picture" class="avatar-upload"/>
              <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
            </el-upload>
            <el-link class="ml10" @click="handlePictureRemove" type="danger">
              {{ i18n('common.delete') }}
            </el-link>
          </el-form-item>

          <el-form-item :label="i18n('wechat.navigation.title')" prop="title">
            <el-input
              v-model="dataForm.title"
              :placeholder="i18n('common.please_input') + i18n('wechat.navigation.title')" clearable></el-input>
          </el-form-item>

          <el-form-item :label="i18n('wechat.navigation.url')" prop="url">
            <el-input
              v-model="dataForm.url" disabled
              :placeholder="i18n('common.please_input') + i18n('wechat.navigation.url')" clearable></el-input>
          </el-form-item>

          <el-form-item :label="i18n('common.sort')" prop="sort">
            <el-input-number v-model="dataForm.sort" :min="0">
            </el-input-number>
          </el-form-item>

          <el-form-item :label="i18n('wechat.navigation.is_visitor')" prop="is_visitor">
            <el-switch
              v-model="dataForm.is_visitor"
              active-value="1"
              :active-text="i18n('common.yes')"
              inactive-value="2"
              :inactive-text="i18n('common.no')">
            </el-switch>
          </el-form-item>

          <el-form-item :label="i18n('common.status')" prop="status">
            <el-switch
              v-model="dataForm.status"
              active-value="1"
              :active-text="i18n('common.enable')"
              inactive-value="2"
              :inactive-text="i18n('common.disable')">
            </el-switch>
          </el-form-item>

          <el-form-item>
            <el-button
              v-if="isAuth('module:wechat:navigation:handle')"
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>

            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="wechat-navigation-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('wechat/navigation')

  const dataForm = reactive({
    id: 0,
    title: '',
    url: '',
    picture: '',
    is_visitor: '1',
    sort: 0,
    status: '1',
  })

  const dataRule = reactive({
    title: [{
      required: true,
      message: i18n('wechat.navigation.rules.title.require'),
      trigger: 'blur',
    }],
    url: [{
      required: true,
      message: i18n('wechat.navigation.rules.url.require'),
      trigger: 'blur',
    }],
    picture: [{
      required: true,
      message: i18n('wechat.navigation.rules.picture.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handlePictureSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.title = data.data.title
            dataForm.url = data.data.url
            dataForm.picture = data.data.picture
            dataForm.is_visitor = data.data.is_visitor.value + ''
            dataForm.sort = data.data.sort
            dataForm.status = data.data.status.value + ''
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            title: dataForm.title,
            url: dataForm.url,
            picture: dataForm.picture,
            is_visitor: dataForm.is_visitor,
            sort: dataForm.sort,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const handlePictureRemove = () => {
    dataForm.picture = ''
  }

  onMounted(() => {
    init(formRef)
  })
</script>
